package cn.tedu.boot311.controller;

import cn.tedu.boot311.mapper.EmpMapper;
import cn.tedu.boot311.vo.Emp;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class EmpController {
    @Autowired(required = false)
    EmpMapper mapper;

    @RequestMapping("/insert")
    public String insert(Emp emp){
        System.out.println(emp);
        mapper.insert(emp);
        return "cg";
    }

    @RequestMapping("/select")
    public String select(){
        List<Emp> list = mapper.select();
        String html = "<table border=1>";
        html += "<caption>成员列表</caption>";
        html += "<tr><th>id</th><th>姓名</th><th>工作</th><th>工资</th><th>操作</th></tr>";
        for (Emp emp : list) {
            html += "<tr>";
            html += "<td>" + emp.getId() + "</td>";
            html += "<td>" + emp.getName() + "</td>";
            html += "<td>" + emp.getJob() + "</td>";
            html += "<td>" + emp.getSal() + "</td>";
            html += "<td><a href='/delete?id=" + emp.getId() + "'>删除</a></td>";
            html += "</tr>";
        }
        html += "</table>";
        return html;
    }

    @RequestMapping("/delete")
    public String delete(int id){
        mapper.delete(id);
        return "删除成功<a href='/select'>返回用户列表</a>";
    }

    @RequestMapping("/update")
    public String update(Emp emp){
        mapper.update(emp);
        return "修改成功<a href='/select'>用户列表</a>";
    }

}
